package com.northone.statistics.activity;

import com.northone.statistics.FragmentController;
import com.northone.statistics.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
	private long firstTime = 0;
	private RadioGroup rg_bottom;

	public RadioGroup getBottomRadioGroup() {
		return rg_bottom;
	}

	private FragmentController controller;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initFragment();
	}

	private void initFragment() {
		controller = FragmentController.getInstance(this, R.id.fm_content);
		controller.replaceFragment(0);
		rg_bottom = (RadioGroup) findViewById(R.id.rg_bottom);
		rg_bottom.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_home:
					controller.replaceFragment(0);
					break;
				case R.id.rb_saint:
					controller.replaceFragment(1);
					break;
				/*
				 * case R.id.rb_metting: controller.replaceFragment(2); break;
				 */
				case R.id.rb_statis:
					controller.replaceFragment(3);
					break;
				default:
					break;
				}
			}
		});
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			long secondTime = System.currentTimeMillis();
			if (secondTime - firstTime > 2000) {
				Toast.makeText(this, R.string.exit_app_tip, Toast.LENGTH_SHORT).show();
				firstTime = secondTime;
				return true;
			} else {
				System.exit(0);
			}
			break;
		}
		return super.onKeyUp(keyCode, event);
	}
}
